package com.jy.wms.controller;

import com.joyi.gate.comm.SessionInfo;
import com.joyi.gate.safety.vo.UserInfo;
import com.jy.wms.common.ExportUtils;
import com.jy.wms.common.base.BaseController;
import com.jy.wms.common.base.BaseSessionInfo;
import com.jy.wms.common.base.JsonResult;
import com.jy.wms.common.base.ServiceException;
import com.jy.wms.pojo.SalesChannelOrderStatusSummary;
import com.jy.wms.pojo.SysHz;
import com.jy.wms.service.SalesChannelOrderStatusSummaryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONObject;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/SalesChannelOrderStatusSummaryController")
@Api(value = "/SalesChannelOrderStatusSummaryController", description = "各销售渠道订单状态汇总")
public class SalesChannelOrderStatusSummaryController extends BaseController<SalesChannelOrderStatusSummaryService, SalesChannelOrderStatusSummary> {

    @ApiOperation(value = "各销售渠道订单状态汇总", response = JSONObject.class, notes = "订单状态汇总")
    @PostMapping(value = "SalesChannel")
    public JSONObject saleschannelorderstatussummary(@RequestBody Map<String, Long> mapp, HttpSession session) throws ServiceException {

        //获取当前操作用户的货主id
        List<SysHz> hz = (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
        String hzid = hz.get(0).getHzid();
        //获取仓库Id
        UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
        String ckid = userinfo.getUser().getOrgId().get(0).toString();

        JSONObject list = baseService.saleschannelorderstatussummary(ckid, hzid, mapp);
        return list;
    }

    @ApiOperation(value = "各销售渠道订单状态汇总报表导出", response = JSONObject.class, notes = "订单状态汇总报表导出")
    @PostMapping(value = "/SalesChannelExcel")
    public @ResponseBody
    JsonResult saleschannelorderstatussummaryExcel(@RequestBody SalesChannelOrderStatusSummary SalesChannelOrderStatusSummary, HttpServletResponse response, HttpServletRequest request, HttpSession session) throws ServiceException {

        String filename = "各销售渠道订单状态汇总报表";
        //获取当前操作用户的货主id
        List<SysHz> hz = (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
        String hzid = hz.get(0).getHzid();
        //获取仓库Id
        UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
        String ckid = userinfo.getUser().getOrgId().get(0).toString();
        SalesChannelOrderStatusSummary.setCkid(ckid);
        SalesChannelOrderStatusSummary.setHzid(hzid);
        try {
            List<SalesChannelOrderStatusSummary> list = baseService.saleschannelorderstatussummary(SalesChannelOrderStatusSummary);

            ExportUtils reportExcel = new ExportUtils();
            reportExcel.excelExport(list, filename, SalesChannelOrderStatusSummary.class, 1, response, request);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return renderSuccess("导出成功");
    }
}
